//
//  MSDaySelectViewController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/11/10.
//  Copyright © 2018 xiangbo. All rights reserved.
//

#import "MSDaySelectViewController.h"

#import "CalenderView.h"

#import "NSDate+Extension.h"

@interface MSDaySelectViewController ()<CalenderViewDelete>

@property (nonatomic, strong) CalenderView *calenderView; // 日历控件
@property (nonatomic, strong) UIView *doneView; // 完成控件
@property (nonatomic, strong) UIButton *doneButton; // 完成按钮

@property (nonatomic, copy) NSString *startDay; // 开始天数
@property (nonatomic, copy) NSString *endDay; // 结束天数

@end

@implementation MSDaySelectViewController

+ (instancetype)loadDaySelectViewControllerWithStartDay:(NSString *)startDay endDay:(NSString *)endDay {
    MSDaySelectViewController *daySelectVC = [[MSDaySelectViewController alloc] init];
    daySelectVC.startDay = startDay;
    daySelectVC.endDay = endDay;
    return daySelectVC;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self initSubViews];
}

#pragma mark - Init Method

- (void)initSubViews {
    
    // 判断是否需要默认startDay和endDay
    if ([MSSafeObject isNull:self.startDay]) {
        // 没有选择日期，赋值默认的日期
        self.startDay = [NSDate currentDay];
        self.endDay = [NSDate tomorrowDay];
    }
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"选择日期";
    [self addBackNavigationBarButtonWithSEL:@selector(backButtonClick:)];
    
    
    CGFloat doneViewHeight = 50;
    [self.view addSubview:self.doneView];
    [self.doneView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(doneViewHeight);
    }];
    
    CGFloat doneWidth = MS_SCREEN_WIDTH - 2 * 20;
    CGFloat doneHeight = 40;
    [self.doneView addSubview:self.doneButton];
    [self.doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.doneView.mas_centerX);
        make.centerY.equalTo(self.doneView.mas_centerY);
        make.width.offset(doneWidth);
        make.height.offset(doneHeight);
    }];
    
    [self.view addSubview:self.calenderView];
    [self.calenderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.offset(0);
        make.bottom.equalTo(self.doneView.mas_top);
    }];
}

#pragma mark - CalenderViewDelete
/** 选中了某个日历 */
- (void)refreshDoneStatus:(BOOL)enable {
    self.doneButton.enabled = enable;
    [self.doneButton setBackgroundColor:enable?MSThemeColor:MSThemeColorA(0.5)];
}

#pragma mark - Event Response
/** 返回按钮点击 */
- (void)backButtonClick:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
/** 完成按钮点击 */
- (void)doneButtonClick {
    if (self.SaveDaySelectorBlock) {
        self.SaveDaySelectorBlock(@{@"current":[self.calenderView newStartSelectDay],
                                    @"tomorrow":[self.calenderView newEndSelectDay]});
    }
    [self backButtonClick:nil];
}

#pragma mark - Private Method

/** 目标结束月份 */
- (NSString *)targetEndMonth {
    NSString *timeStr = [NSDate currentDay];
    NSInteger year = [[timeStr substringToIndex:4] integerValue];
    NSInteger month = [[timeStr substringWithRange:NSMakeRange(5, 2)] integerValue];
    NSInteger day = [[timeStr substringFromIndex:8] integerValue];
    NSInteger addMonth = 6; // 添加的月份
    if (month + addMonth > 12) {
        month = (month + addMonth)%12;
        year += 1;
    } else {
        month += addMonth;
    }
    NSString *newTimeStr = [NSString stringWithFormat:@"%ld-%02ld-%02ld", year, month, day];
    return [MSSafeObject stringInfo:newTimeStr];
}

#pragma mark - Getters

- (CalenderView *)calenderView {
    if (!_calenderView) {
        _calenderView = [[CalenderView alloc] initWithFrame:CGRectZero startDay:[NSDate currentDay] endDay:[self targetEndMonth] startSelectDay:self.startDay endSelectDay:self.endDay];
        _calenderView.delegate = self;
        _calenderView.yearMonthFormat = @"%zd年%02zd月";
        _calenderView.showWeekBottomLine = YES;
    }
    return _calenderView;
}

- (UIView *)doneView {
    if (!_doneView) {
        _doneView = [[UIView alloc] init];
        _doneView.backgroundColor = [UIColor whiteColor];
    }
    return _doneView;
}

- (UIButton *)doneButton {
    if (!_doneButton) {
        _doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_doneButton setTitle:@"完成" forState:UIControlStateNormal];
        [_doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_doneButton setBackgroundColor:MSThemeColor];
        _doneButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_doneButton addTarget:self action:@selector(doneButtonClick) forControlEvents:UIControlEventTouchUpInside];
        _doneButton.layer.cornerRadius = 3.0;
    }
    return _doneButton;
}

#pragma mark -  Memory Method

- (void)dealloc {
    NSLog(@"%s", __func__);
}

@end
